package com.shadow.bus.tools;

import android.app.Activity;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import java.util.HashMap;

import static android.content.Context.USB_SERVICE;

/**
 *
 * @author zhangyu
 * @date 2017/9/28
 */

public class UsbUtil {
    private static final int m_nVID = 1306;
    private static final int m_nPID = 20763;
    private static UsbDevice m_LotusCardDevice = null;
    /**
     * 设置USB读写回调 串口可以不用此操作
     *
     * @return
     */
    public static boolean hasUsbDevice(Activity context) {
        UsbManager m_UsbManager = (UsbManager) context.getSystemService(USB_SERVICE);
        if (null == m_UsbManager) {
            return false;
        }
        HashMap<String, UsbDevice> deviceList = m_UsbManager.getDeviceList();
        if (!deviceList.isEmpty()) {
            for (UsbDevice device : deviceList.values()) {
                if ((m_nVID == device.getVendorId())
                        && (m_nPID == device.getProductId())) {
                    m_LotusCardDevice = device;
                    break;
                }
            }
        }
        return null != m_LotusCardDevice;
    }

}
